<!--
 * @author hemengxia
 企业账号管理-交易记录--导出
-->
<template>
  <section class="transaction-record-statistics">
    <section class="main-search">
      <div class="search-label">统计时间</div>
      <el-row :gutter="10">
        <el-col :span="18">
          <section class="search-item">
            <el-date-picker
              v-model="search.time"
              range-separator="至"
              start-placeholder="统计开始时间"
              end-placeholder="统计结束时间"
              clearable
              :style="{width:'100%'}"
              type="daterange"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd HH:mm:ss"
            />
          </section>

        </el-col>
        <el-col :span="6">

          <section class="search-item" style="text-align: center;">
            <el-button type="primary" @click="handleStart">开启统计任务</el-button>
            <el-button type="primary" @click="handleReset">重置</el-button>
          </section>
        </el-col>
      </el-row>
    </section>
    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>交易记录统计</span>
      </section>
      <section class="main-buttons-btns">
        <el-button style="padding: 10px 30px;" @click="handleCancel()">返回</el-button>
      </section>
    </section>
    <section class="main-table">
      <v-table
        :list="list"
        tag="transactionStatistics"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        :selection="false"
      >
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              :disabled="scope.row.status === 0"
              type="text"
              size="mini"
              @click="handleDelete(scope.row)"
            >删除</el-button>
            <el-button type="text" size="mini" @click="handleExportFire(scope.row)">导出</el-button>
            <el-button type="text" size="mini" @click="handleAgain(scope.row)">重新统计</el-button>
            <!--            <el-button type="text" size="mini" @click="handleView(scope.row)">查看明细</el-button>-->
          </template>
        </el-table-column>
      </v-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </section>
    <el-drawer title="统计详情" :visible.sync="drawer" direction="ttb" size="100%" destroy-on-close>
      <v-view :id="id" :obj="obj" />
    </el-drawer>
  </section>
</template>
<script>
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import VView from './../components/view'
import { insert, again, exportFile, queryPage, del } from './../api/statistics'
import { exportSign } from '@/utils/fileImportOrExportSign'
export default {
  name: 'TransactionStatistics',
  components: { Pagination, VTable, VView },
  data() {
    return {
      search: {
        time: []
      },
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      listLoading: false,
      list: [],
      drawer: false,
      id: '',
      obj: {}
    }
  },
  created() {
    this.id = this.$route.query.i
    this.getList()
  },
  methods: {
    handleCancel() {
      this.$router.back()
    },
    filtercolumn(scope, item) {
      return scope.row[item]
    },

    handleStart() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      const params = {
        accountId: this.id
      }
      params['startDate'] = this.search.time && this.search.time.length > 0 ? this.search.time[0] : ''
      params['endDate'] = this.search.time && this.search.time.length > 0 ? this.search.time[1] : ''
      return new Promise((resolve, reject) => {
        insert(params).then(() => {
          loading.close()
          this.$message({
            title: '成功',
            message: '统计任务开启成功',
            type: 'success',
            duration: 2000
          })
          this.handleReset()
          this.listQuery.page = 1
          this.getList()
        }).catch(error => {
          loading.close()
          reject(error)
        })
      })
    },

    handleReset() {
      this.search.time = []
    },

    handleExportFire(row) {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      const query = exportSign({ id: row.id }).obj
      exportFile(query).then((res) => {
        loading.close()
        const content = res.data
        const blob = new Blob([content])
        const fileName = '交易记录统计.xlsx'
        if ('download' in document.createElement('a')) {
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else {
          navigator.msSaveBlob(blob, fileName)
        }
      }).catch(() => {
        loading.close()
      })
    },

    handleView(row) {
      const obj = {
        org: row.cndDeptIdsValue ? row.cndDeptIdsValue.split(',') : '',
        name: row.cndUserRealName,
        phone: row.cndUserphoneNumber,
        all: row.cndDeptUserSubFlag,
        down: row.cndDeptSubFlag,
        startTime: row.cndStartTime,
        endTime: row.cndEndTime,
        rush: row.cndBreakthroughIdsValue ? row.cndBreakthroughIdsValue.split(',') : ''
      }
      this.obj = Object.assign({}, obj)
      this.id = row.id
      this.drawer = true
    },

    handleAgain(row) {
      this.$confirm('确定要重新统计吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          again({ id: row.id, accountId: row.accountId }).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '重新统计成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch(() => {
      })
    },

    handleDelete(row) {
      this.$confirm('确定删除该统计任务吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return new Promise((resolve, reject) => {
          del({ ids: row.id }).then(() => {
            this.$message({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            reject(error)
          })
        })
      }).catch(() => {
      })
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        accountId: this.id
      }
      return new Promise((resolve, reject) => {
        queryPage(query).then(response => {
          this.list = response.data.data.rows
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
.transaction-record-statistics {
  padding: 10px;

  .main-search {
    background-color: #fff;
    padding: 10px;

    .search-label {
      height: 30px;
      line-height: 30px;
      font-size: 14px;
      color: #a6a6a6;
      font-weight: 400;
    }

    .search-item {
      margin-bottom: 10px;

      &.button {
        text-align: center;
      }

      .item-label {
        height: 30px;
        line-height: 30px;
        font-size: 14px;
        color: #a6a6a6;
        font-weight: 400;
      }

      .el-input {
        position: relative;

        &.el-date-editor input {
          padding-left: 30px;
        }

        .el-input__inner {
          border: none;
          padding: 0;
        }

        &:after {
          content: '';
          height: 1px;
          background-color: #e2e3e8;
          position: absolute;
          left: 0;
          right: 0;
          bottom: 0;
        }
      }
    }
  }

  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-top: 10px;

    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }

  /* .main-table {
     background-color: #fff;
     .table-title {
       display: flex;
       align-items: center;
       justify-content: space-between;
       padding: 0 10px;
       .title-txt {
         font-size: 16px;
         font-weight: bold;
         color: rgba(51, 51, 51, 1);
       }
     }
   }*/

  .el-drawer__wrapper {
    .el-drawer__header {
      margin-bottom: 10px;
    }

    .el-drawer__header span:focus {
      outline: none;
    }

    .el-drawer__close-btn:focus {
      outline: none;
    }
  }
}
</style>
